classdef TestMexUDPMxGramDataTypes < TestMxGram
    % reuse all the mxGram tests for mexUDPMxGram

    properties
        sendSocket;
        receiveSocket;
    end
    
    methods
        function self = TestMexUDPMxGramDataTypes(name)
            self = self@TestMxGram(name);
        end
        
        function setUp(self)
            self.setUp@TestMxGram;
            clear mex
            
            loopback = '127.0.0.1';
            sendPort = 6665;
            receivePort = 6666;
            
            self.sendSocket = mexUDPMxGram('open', ...
                loopback, loopback, sendPort, receivePort);
            
            self.receiveSocket = mexUDPMxGram('open', ...
                loopback, loopback, receivePort, sendPort);
        end
        
        function tearDown(self)
            mexUDPMxGram('closeAll');
            self.sendSocket = -1;
            self.receiveSocket = -1;
        end
        
        function remade = roundTrip(self, original)
            status = mexUDPMxGram('send', self.sendSocket, original);
            assertTrue(status > 0, sprintf('send error: %d', status))
            
            pause(.005);
            
            [remade, status] = mexUDPMxGram('receive', self.receiveSocket);
            assertTrue(status > 0, sprintf('receive error: %d', status))
            assertEqual(original, remade, ...
                'should have reproduced original variable')
        end
        
        function failWithUnreasonableInput(self, unreasonable)
            status = mexUDPMxGram('send', self.sendSocket, unreasonable);
            assertFalse(status > 0, ...
                'should return negative error code for unreasonable input')
        end
        
        function testNoArgs(self)
            % should print usage examples
            mexName = 'mexUDPMxGram';
            result = evalc(mexName);
            assertFalse(isempty(strfind(result, mexName)), 'no-args should print usage string')
        end
    end
end